﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;

namespace GTA
{
    public partial class FrmPerformanceArea : BaseForm
    {
        #region Local Objects
        List<BusinessEntity.TaskPerformance> performancesFound;
        #endregion

        #region Starting Form
        public FrmPerformanceArea()
        {
            InitializeComponent();
        }
        private void FrmPerformanceArea_Load(object sender, EventArgs e)
        {
            StartForm();
        }
        private void StartForm()
        {
            try
            {
                SetStartDatesFilter();
                RestoreSearchFilter();
            }
            catch (Exception ex)
            {
                HandleException("Inicializção da tela.", ex);
            }
        }
        #endregion

        #region Events
        private void lstPerformance_ColumnClick(object sender, ColumnClickEventArgs e)
        {
            SortList(lstPerformance, GetColumnTypeLstPerformance(e.Column), ref columnOrder, ref sortOrder, e.Column);
        }
        private void cmdSearch_Click(object sender, EventArgs e)
        {
            SearchPerformance();
        }
        private void lstPerformance_Resize(object sender, EventArgs e)
        {
            lstPerformance.Columns[0].Width = lstPerformance.Width - lstPerformance.Columns[1].Width - lstPerformance.Columns[2].Width - lstPerformance.Columns[3].Width - 20;
        }
        private void cmdPrint_Click(object sender, EventArgs e)
        {
            PrintArchitecturePerformance();
        }
        private void cmdExportXLS_Click(object sender, EventArgs e)
        {
            ExportXLSArchitecturePerformance();
        }
        private void cmdCleanFilter_Click(object sender, EventArgs e)
        {
            CleanFilter();
        }
        #endregion

        #region Methods
        private OrdenacaoListView.Tipo GetColumnTypeLstPerformance(Int32 column)
        {
            OrdenacaoListView.Tipo tipoColuna;
            switch (column)
            {
                case 0:
                    tipoColuna = OrdenacaoListView.Tipo.Alfabetica;
                    break;
                case 1:
                    tipoColuna = OrdenacaoListView.Tipo.Numerica;
                    break;
                case 2:
                    tipoColuna = OrdenacaoListView.Tipo.Numerica;
                    break;
                case 3:
                    tipoColuna = OrdenacaoListView.Tipo.Decimal;
                    break;
                default:
                    tipoColuna = OrdenacaoListView.Tipo.Alfabetica;
                    break;
            }
            return tipoColuna;
        }
        private void SetStartDatesFilter()
        {
            txtFilterDateIni.Value = DateTime.Now.AddDays(-30);
            txtFilterDateEnd.Value = DateTime.Now.AddDays(30);
            txtFilterDateIni.Checked = false;
            txtFilterDateEnd.Checked = false;
        }
        private void CleanFilter()
        {
            try
            {
                SetStartDatesFilter();
                ucSearchFilter.CleanFilter();
                SaveFilter();
            }
            catch (Exception ex)
            {
                HandleException("Limpar o filtro da pesquisa", ex);
            }
        }
        private void SearchPerformance()
        {
            try
            {
                Cursor = Cursors.WaitCursor;
                BusinessLogic.GTA bl = new BusinessLogic.GTA();
                BusinessEntity.TaskFilter filter = GetSearchFilter();
                List<BusinessEntity.TaskPerformance> performances = bl.ListPerformance(filter, Program.LoggedUser);
                performancesFound = performances;
                lstPerformance.Items.Clear();
                if (performances != null && performances.Count > 0)
                {
                    foreach (BusinessEntity.TaskPerformance itemPerformance in performances)
                    {
                        ListViewItem item = new ListViewItem(itemPerformance.Architect.Name);
                        item.SubItems.Add(itemPerformance.Delivered.ToString());
                        item.SubItems.Add(itemPerformance.Overdue.ToString());
                        item.SubItems.Add(itemPerformance.Percentual.ToString("N2"));
                        lstPerformance.Items.Add(item);
                    }
                }
                else
                    MessageBox.Show("Nenhum retorno!", "Alerta", MessageBoxButtons.OK, MessageBoxIcon.Information);
                SaveFilter();
            }
            catch (Exception ex)
            {
                HandleException("Pesquisar Performance de Arquitetura", ex);
            }
            finally
            {
                Cursor = Cursors.Default;
            }
        }
        private BusinessEntity.TaskFilter GetSearchFilter()
        {
            BusinessEntity.TaskFilter filter = new GTA.BusinessEntity.TaskFilter();
            filter.Project = ucSearchFilter.GetSelectedProjects();
            filter.DesignedTo = ucSearchFilter.GetSelectedResponsibles();
            if (txtFilterDateIni.Checked)
                filter.ScheduleEstimatedEndFrom = txtFilterDateIni.Value;
            if (txtFilterDateEnd.Checked)
                filter.ScheduleEstimatedEndTo = txtFilterDateEnd.Value;
            return filter;
        }
        private void PrintArchitecturePerformance()
        {
            try
            {
                if (performancesFound != null && performancesFound.Count > 0)
                {
                    BusinessLogic.GTA bl = new BusinessLogic.GTA();
                    String htmlContent = bl.GetPerformanceHTMLFormat(performancesFound);
                    FrmPrint wd = new FrmPrint(htmlContent);
                    wd.Show(this);
                }
            }
            catch (Exception ex)
            {
                HandleException("Imprimir resultado da pesquisa", ex);
            }
        }
        private void ExportXLSArchitecturePerformance()
        {
            try
            {
                if (performancesFound != null && performancesFound.Count > 0)
                {
                    if (saveFileDialog.ShowDialog() == DialogResult.OK)
                    {
                        String pathXlsFile = saveFileDialog.FileName;
                        BusinessLogic.GTA bl = new BusinessLogic.GTA();
                        bl.ExportXlsPerformance(performancesFound, pathXlsFile);
                        if (MessageBox.Show("Arquivo salvo com sucesso.\r\n\r\nDeseja abrir o arquivo!?", "Confirmação", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.Yes)
                            System.Diagnostics.Process.Start(pathXlsFile);
                    }
                }
            }
            catch (Exception ex)
            {
                HandleException("Exportar resultado da pesquisa para XLS", ex);
            }
        }
        private void SaveFilter()
        {
            try
            {
                BusinessEntity.TaskFilter filter = GetSearchFilter();
                Program.SystemConfiguration.FilterSearchFrmPerformanceArea = new GTA.BusinessEntity.Configuration.SearchFilter(filter);
                Program.SaveSystemConfiguration();
            }
            catch (Exception ex)
            {
                HandleException("Salvar o filtro da pesquisa.", ex);
            }
        }
        private void RestoreSearchFilter()
        {
            try
            {
                if (Program.SystemConfiguration.FilterSearchFrmPerformanceArea != null)
                {
                    ucSearchFilter.RestoreSearchFilterProjects(Program.SystemConfiguration.FilterSearchFrmPerformanceArea.Project);
                    ucSearchFilter.RestoreSearchFilterResponsibles(Program.SystemConfiguration.FilterSearchFrmPerformanceArea.DesignedTo);
                    if (Program.SystemConfiguration.FilterSearchFrmPerformanceArea.ScheduleEstimatedEndFrom != DateTime.MinValue)
                    {
                        txtFilterDateIni.Checked = true;
                        txtFilterDateIni.Value = Program.SystemConfiguration.FilterSearchFrmPerformanceArea.ScheduleEstimatedEndFrom;
                    }
                    if (Program.SystemConfiguration.FilterSearchFrmPerformanceArea.ScheduleEstimatedEndTo != DateTime.MinValue)
                    {
                        txtFilterDateEnd.Checked = true;
                        txtFilterDateEnd.Value = Program.SystemConfiguration.FilterSearchFrmPerformanceArea.ScheduleEstimatedEndTo;
                    }
                }
            }
            catch (Exception ex)
            {
                HandleException("Restaurar os últimos filtros utilizados.", ex);
            }
        }
        #endregion
    }
}
